<?php
defined('IN_PHPCMS') or exit('No permission resources.');
if(!defined('CACHE_MODEL_PATH')) define('CACHE_MODEL_PATH',PHPCMS_PATH.'caches'.DIRECTORY_SEPARATOR.'caches_model'.DIRECTORY_SEPARATOR.'caches_data'.DIRECTORY_SEPARATOR);

pc_base::load_sys_class('model', '', 0);
class reg_table_info_model extends model {
	public function __construct() {
		$this->db_config = pc_base::load_config('database');
		$this->db_setting = 'default';
		$this->table_name = 'reg_table_info';
		parent::__construct();
	}

	/**
	 * 添加修改
	 */
	public function edit ($data,$id='')
	{
		$data = $this->check($data);
		if (!$id) {
			$id = $this->insert($data, true);
		} else {
			$ids = $this->update($data, array('id'=>$id));
		}
		return $id;
	}

	/**
	 * 取报名列表
	 */
	public function regList ()
	{
		$where = array();
		$where['status'] = 1;
		$where['siteid'] = get_siteid();
		$list = array();
		if($_SESSION['roleid'] == 1 ) {
			$reg_list = $this->select($where);
			foreach ($reg_list as $v) {
				$list[$v['id']] = $v['title'];
			}
		} else {
			$reg_auth = pc_base::load_model('reg_auth_model');
			$reg_list = $reg_auth->select(array('userid'=>$_SESSION['userid']));
			foreach ($reg_list as $v) {
				$info = $this->get_one(array('id'=>$v['reg_id']));
				$list[$v['reg_id']] = $info['title'];
			}
		}
		return $list;
	}

	/**
	 * 表单验证
	 *
	 * @param array $data
	 *            表单传递的值
	 * @param string $a
	 *            add/edit添加操作时，自动加上默认值
	 */
	public function check($data, $a = 'add')
	{
		if($data['source_type'] == 1) {
			$data['source_id'] = $_POST['contestid'];
		} else if ($data['source_type'] == 2) {
			$data['source_id'] = $_POST['courseid'];
		}
		if ($a == 'add') {
			$data['siteid'] = get_siteid();
			$data['create_time'] =date("Y-m-d H:i:s",time());
			$data['created_by'] = $_SESSION['userid'];
			$data['update_time'] =date("Y-m-d H:i:s",time());
			$data['updated_by'] = $_SESSION['userid'];
		} else {
			$data['update_time'] =date("Y-m-d H:i:s",time());
			$data['updated_by'] = $_SESSION['userid'];
		}
		return $data;
	}

}
?>